/******************* 公共变量 *******************/
var userId = core.getUrlData('user_id'),
	type = core.getUrlData('type'),
	$img,
	$updateImg,
	updateImg,
	cropper;

$('html body .wrap .user-header').height(winHeight - headerTitleHeight - $('html body .header-footer').height());

/******************* Vue初始化代码 *******************/
var vm = new Vue({
	el: '.wrap',
	data: {
		id: '',
		userId: userId,
		updateImg: true,
		iOS: $.os.ios
	},
	methods: {
		addImage: function (event) {
			if (cropper && updateImg.files && updateImg.files.length) {
				if (/^image\/\w+/.test(updateImg.files[0].type)) {
					cropper.reset().replace(URL.createObjectURL(updateImg.files[0]));
				} else {
					core.layerHint('请选择图片文件');
				}
			}
		},
		save: function () {
			var _this = this;
			if (this.updateImg) {
				this.updateImg = false;
				layer.open({
					type: 2,
					shadeClose: false,
					className: 'loading',
					content: '图片上传中，请稍候',
					success: function () {
						var canvas = cropper.getCroppedCanvas();
						// console.log(canvas.toDataURL('image/jpeg', 0.7))
						// $('p').eq(0).html(canvas.toDataURL('image/jpeg'))
						// $('p').eq(1).html(canvas.toDataURL('image/jpeg', 0.8))
						// $('p').eq(2).html(canvas.toDataURL('image/jpeg', 0.7))
						// $('p').eq(3).html(canvas.toDataURL('image/jpeg', 0.6))
						$.ajax({
							url: userUpdateUrl,
							type: 'post',
							data: {
								photo_id: canvas.toDataURL('image/jpeg', 0.7)
								// photo_id: '../../../Public/mobile/images/nihao1.jpg'
							},
							success: function(data) {
								console.log(data)

								layer.closeAll();
								if (data.result == 1) {
									if (type == 'complete') {
										core.hintHref(data.message, indexUrl);
									} else {
										core.hintHref(data.message, userDetail + '?&is_self=1&user_id=' + _this.id);
									}
								} else {
									core.layerHint(data.message);
								}
								_this.updateImg = true;
							}
						});
					}
				});
			}
		}
	},
	computed: {
		
	},
	components: {
		
	},
	created: function () {
	},
	mounted: function () {
		common.bindBack();
		$('html body .header-footer span').on('tap', function () {
			core.back();
		});
		$updateImg = $('#updateImage');
		updateImg = $updateImg[0],
		$img = $('html body .wrap .user-header section img');
		$.ajax({
			url: userInfoUrl,
			type: 'post',
			// data: {user_id: userId},
			success: function (data) {
				console.log(data)
				$.extend(vm, data.list);
				$img.attr('src', data.list.photo_id);
				cropper = new Cropper($img[0], {
					aspectRatio: 1 / 1,
					viewMode: 1,
					crop: function(e) {
						// console.log(e.detail.x);
						// console.log(e.detail.y);
						// console.log(e.detail.width);
						// console.log(e.detail.height);
						// console.log(e.detail.rotate);
						// console.log(e.detail.scaleX);
						// console.log(e.detail.scaleY);
					}
				});
			}
		});
	}
});

